from model.province_city import ProvinceCity
from sqlalchemy import asc, and_

class ProvinceService(object):
    def __init__(self, session):
        self.session = session

    def add(self, job):
        self.session.add(job)

    def get_by_id(self, pc_id):
        return self.session.query(ProvinceCity).filter(ProvinceCity.pc_id == pc_id).one()


    """获得顶级城市信息"""

    def list_province(self):
        q = self.session.query(ProvinceCity).filter(ProvinceCity.parent_id == 0)
        q = q.order_by(asc(ProvinceCity.code))
        return q.all()

    def list_info(self):
        m_list = []
        q = self.session.query(ProvinceCity)
        q = q.order_by(asc(ProvinceCity.code))
        result = q.all()
        if result:
            for d in result:
                m_list.append({"pc_id": d.pc_id,
                               "code": d.code,
                               "name": d.name,
                               "parent_id": int(d.parent_id),
                               "first_letter": d.first_letter,
                               "level": d.level
                               })
            return m_list
        else:
            return None

    def list_city(self):
        q = self.session.query(ProvinceCity).filter(and_(ProvinceCity.parent_id != 0, ProvinceCity.level == 1))
        q = q.order_by(asc(ProvinceCity.code))
        return q.all()

    def list_area(self):
        q = self.session.query(ProvinceCity).filter(and_(ProvinceCity.parent_id != 0, ProvinceCity.level == 2))
        q = q.order_by(asc(ProvinceCity.code))
        return q.all()

    def list_area_dict(self):
        # class_code : [{}]
        f_dict = {}
        a_list = self.list_area()
        if a_list:
            for d in a_list:
                s_list = []
                ##默认增加一个全部
                s_list.append({"code": "", "name": "全部"})
                if not f_dict.get(str(d.parent_id)):
                    s_list.append({"code": d.code, "name": d.name})
                    f_dict[d.parent_id] = s_list
                else:
                    s_list = f_dict.get(str(d.parent_id))

                    s_list.append({"code": d.code, "name": d.name})
            return f_dict
        else:
            return None

    def list_city_dict(self):
        # class_code : [{}]
        f_dict = {}
        area_dict = self.list_area_dict()
        r_list = self.list_city()
        if r_list:
            for d in r_list:
                s_list = []
                if not f_dict.get(str(d.parent_id)):
                    s_list.append({"code": d.code, "name": d.name,
                                   "children": area_dict.get(str(d.pc_id))})
                    f_dict[d.parent_id] = s_list
                else:
                    s_list = f_dict.get(str(d.parent_id))

                    s_list.append({"code": d.code, "name": d.name,
                                   "children": area_dict.get(str(d.pc_id))})
            return f_dict
        else:
            return None

    def list_all(self):
        data_s = []
        result = self.list_province()
        if result:
            city_dict = self.list_city_dict()
            for d in result:
                data_s.append(
                    {"code": d.code,
                     "name": d.name,
                     "children": city_dict.get(str(d.pc_id))
                     }
                )
            return data_s
        else:
            return None


if __name__ == '__main__':
    # s = get_session()
    # dao = ProvinceService(s)
    # print(dao.list_info())
    pass
